﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SpaceShooter.Extensions;

namespace SpaceShooter.Components
{
    public class CompositeComponent : DrawableGameComponent, IList<DrawableGameComponent>
    {
        private IList<DrawableGameComponent> components = new List<DrawableGameComponent>();

        public CompositeComponent(Game game)
            : base(game)
        {
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            this.components.Foreach(x => x.Draw(gameTime));
        }

        public override void Initialize()
        {
            base.Initialize();

            this.components.Foreach(x => x.Initialize());
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            this.components.Foreach(x => x.Update(gameTime));
        }

        #region IList<DrawableGameComponent> Members

        public int IndexOf(DrawableGameComponent item)
        {
            return this.components.IndexOf(item);
        }

        public void Insert(int index, DrawableGameComponent item)
        {
            this.components.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            this.components.RemoveAt(index);
        }

        public DrawableGameComponent this[int index]
        {
            get
            {
                return this.components[index];
            }
            set
            {
                this.components[index] = value;
            }
        }

        #endregion

        #region ICollection<DrawableGameComponent> Members

        public void Add(DrawableGameComponent item)
        {
            this.components.Add(item);
        }

        public void Clear()
        {
            this.components.Clear();
        }

        public bool Contains(DrawableGameComponent item)
        {
            return this.components.Contains(item);
        }

        public void CopyTo(DrawableGameComponent[] array, int arrayIndex)
        {
            this.components.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.components.Count; }
        }

        public bool IsReadOnly
        {
            get { return this.components.IsReadOnly; }
        }

        public bool Remove(DrawableGameComponent item)
        {
            return this.components.Remove(item);
        }

        #endregion

        #region IEnumerable<DrawableGameComponent> Members

        public IEnumerator<DrawableGameComponent> GetEnumerator()
        {
            return this.components.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.components.GetEnumerator();
        }

        #endregion
    }
}
